<?php
##############################################
#
#   结算页
# @ Time : 2017-3-1 - 下午11:55:14
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Home\Controller;
use Think\Controller;
use Home\Model\ActivityOrderModel;
use Home\Model\ActivityModel;

class PaymentController extends BaseController{
	private $_order;
	
	public function _initialize(){
	    parent::_initialize();
	    $this->_order = new ActivityOrderModel();
	}
	
	/**
	 * 提交活动订单
	 */
	public function addOrder(){
	    $data = I('post.');
	    $activity_model = new ActivityModel();
	    $activity_data = $activity_model->getActivityDetail($data['activity_id']);
	    $data['user_id'] = $this->user_id;
	    $data['activity_name'] = $activity_data['activity_name'];
	    $data['order_status'] = 1;
	    $data['payment_status'] = 1;
	    $data['number'] = 1;
	    $data['amount'] = $activity_data['price'] * $data['number'];
	    $data['order_sn'] = self::createOrder_sn();
	    $data['add_time'] = time();
	    $row = $this->_order->addOrder($data);
	    if($row){
	       /*  $content = "恭喜您刚刚报名参加了[ {$activity_data['activity_name']} ] 的活动!";
	        $jsApi = new \JsApi_pub();
	        $jsApi->push_msg($this->openid, $content); */
	        //echo 'ok';
	        $this->redirect('Wxpay/js_api_call',array('order_sn' => $data['order_sn']));
	        exit;
	    }else{
	        header('location:'.__ROOT__.'/');
	    }
	}
	
	private function createOrder_sn(){
	    //订购日期
	    $order_date = date('Y-m-d');
	    //订单号码主体（YYYYMMDDHHIISSNNNNNNNN）
	    $order_id_main = date('YmdHis') . rand(10000000,99999999);
	    //订单号码主体长度
	    $order_id_len = strlen($order_id_main);
	    $order_id_sum = 0;
	    for($i=0; $i<$order_id_len; $i++){
	        $order_id_sum += (int)(substr($order_id_main,$i,1));
	    }
	    //唯一订单号码（YYYYMMDDHHIISSNNNNNNNNCC）
	    $order_id = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100,2,'0',STR_PAD_LEFT);
	    return $order_id;
	}
	
	
}